<?php

namespace App\Models\Admin\Framework;

use App\Master\Framework\Extend\DataUtil;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class AdminMenusModel extends Model
{
    use HasFactory;

    protected $table      = 'admin_menus';
    public    $timestamps = false;

    /**
     * 获取菜单树形结构
     * @param array $status
     * @return array
     */
    public static function getMenuRecursion($status = [1])
    {
        $menus = self::query()->whereIn('status', $status)->where('is_delete', 0)
            ->orderBy('sort', 'asc')->orderBy('id', 'asc')->get()->toArray();
        if (empty($menus)) {
            return [
                'rows'  => [],
                'total' => 0
            ];
        }
        return [
            'rows'  => DataUtil::recursion($menus, 'id', 'parent_id'),
            'total' => self::query()->whereIn('status', $status)->where('is_delete', 0)->count()
        ];
    }

    public static function insertMenu($params, $cud = '')
    {
        if (!$params['name']) {
            return false;
        }

        $params['level'] = 1;

        if ($parent_ids = is_array($params['parent_id']) ? $params['parent_id'] : [$params['parent_id']]) {
            $params['parent_id'] = end($parent_ids) ? end($parent_ids) : 0;
            $params['level']     = count($parent_ids) + 1;
        } else {
            $params['parent_id'] = 0;
        }

        $params['facility'] = $params['facility'] ? intval($params['facility']) : 1;
        $params['status']   = $params['status'] == 'true' ? 1 : 2;
        //        $params['module']      = $params['module'] == 'true' ? 1 : 0;
        //        $params['extend_page'] = (isset($params['extend_page']) && $params['extend_page'] == 'true') ? 1 : 0;
        $params['path']   = $params['path'] ?? '#';
        $params['action'] = $params['action'] ?? '';
        $params['icon']   = $params['icon'] ?? '';
        //        $params['module_type'] = $params['module_type'] ?? '';
        $params['created_at'] = time();

        if (!$menu_id = self::query()->insertGetId($params)) {
            return false;
        }

        self::_CreateMenuCurdSubmenu($params, $menu_id, $cud);

        return $menu_id;
    }

    public static function updateMenu($params, $menu_id)
    {
        if (!$params['name'] || !$menu_id) {
            return false;
        }

        $params['level'] = 1;
        if ($parent_ids = is_array($params['parent_id']) ? $params['parent_id'] : [$params['parent_id']]) {
            $params['parent_id'] = end($parent_ids) ? end($parent_ids) : $parent_ids;
            $params['level']     = count($parent_ids) + 1;
        } else {
            $params['parent_id'] = 0;
        }
        $params['facility'] = $params['facility'] ? intval($params['facility']) : 1;
        $params['status']   = $params['status'] == 'true' ? 1 : 2;
        //        $params['module']      = $params['module'] == 'true' ? 1 : 0;
        //        $params['extend_page'] = (isset($params['extend_page']) && $params['extend_page'] == 'true') ? 1 : 0;
        $params['path']       = $params['path'] ?? '#';
        $params['action']     = $params['action'] ?? '';
        $params['icon']       = $params['icon'] ?? '';
        $params['updated_at'] = time();
        if (!self::query()->where('id', $menu_id)->update($params)) {
            return false;
        }
        return $menu_id;
    }

    protected static function _CreateMenuCurdSubmenu($params, $menu_id, $cud)
    {
        if (!$cud = array_flip($cud)) {
            return true;
        }

        $menus_map = [
            'insert'   => ['name' => '添加', 'action' => 'insert', 'icon' => 'el-icon-circle-plus-outline'],
            'update'   => ['name' => '编辑', 'action' => 'update', 'icon' => 'el-icon-edit'],
            'remove'   => ['name' => '删除', 'action' => 'remove', 'icon' => 'el-icon-delete'],
            'status'   => ['name' => '状态', 'action' => 'status', 'icon' => 'el-icon-warning-outline'],
            'export'   => ['name' => '导出', 'action' => 'export', 'icon' => 'el-icon-download'],
            'deletion' => ['name' => '批量删除', 'action' => 'deletion', 'icon' => 'el-icon-delete']
        ];

        if (!$cud_arr = array_intersect_key($menus_map, $cud)) {
            return true;
        }

        $menus_arr = [
            'parent_id' => $menu_id,
            'name'      => '添加',
            'path'      => $params['path'] . '/insert',
            'action'    => 'insert',
            'icon'      => '',
            'level'     => $params['level'] + 1,
            'status'    => 1,
            'facility'  => 2,
            'sort'      => 255
        ];

        //        if ($params['extend_page']) {
        //            array_push($cud_arr, ['name' => '列表设置', 'action' => 'extend_page', 'icon' => 'fa fa-cogs']);
        //        }

        foreach ($cud_arr as $key => $value) {
            $cud_arr[$key]         = array_merge($menus_arr, $value);
            $cud_arr[$key]['path'] = $params['path'] . '/' . $value['action'];
        }

        unset($params, $menus_arr, $menus_map, $cud);

        self::query()->insert(array_values($cud_arr));

        return true;
    }

}
